package com.zhengsr.latte.ec.ec.sign;

import android.support.design.widget.TextInputEditText;
import android.util.Patterns;

import com.zhengsr.latte.ec.R;
import com.zhengsr.latte.ec.R2;
import com.zhengsr.latte.net.RestClient;
import com.zhengsr.latte.net.callback.RestListenerAdapter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhengshaorui on 2018/4/23.
 */

public class SignInDelegate extends BaseSignDelegate {
    private static final String TAG = "SignInDelegate";

    @BindView(R2.id.sign_in_email)
    TextInputEditText mEmail;
    @BindView(R2.id.sign_in_passwrod)
    TextInputEditText mPassword;



    @OnClick(R2.id.sign_up_link_tv)
    void onClickLinkSignUp(){
        startWithPop(new SignUpDelegate());
    }
    @OnClick(R2.id.sign_in_btn)
    void onClickSignIn(){
        if (checkForm()){
            RestClient client = new RestClient.Builder()
                    .setUrl("login")
                    .setParams("email",mEmail.getText().toString())
                    .setParams("password",mPassword.getText().toString())
                    .setListenerAdapter(new RestListenerAdapter() {
                        @Override
                        public void onSuccess(String reposonse) {
                            super.onSuccess(reposonse);
                            mISignCallback.onSignInSuccess();
                        }

                        @Override
                        public void onFailure(String msg) {
                            super.onFailure(msg);
                        }

                        @Override
                        public void onError(int code, String errormsg) {
                            super.onError(code, errormsg);
                        }
                    })
                    .builder()
                    .post();
        }
    }

    private boolean checkForm(){
        final String email = mEmail.getText().toString();
        final String password = mPassword.getText().toString();

        boolean isPass = true;

        if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            mEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEmail.setError(null);
        }



        if (password.isEmpty() || password.length() < 6) {
            mPassword.setError("请填写至少6位数密码");
            isPass = false;
        } else {
            mPassword.setError(null);
        }



        return isPass;
    }

    @Override
    public Object getLayout() {
        return R.layout.sign_in_layout;
    }
}
